/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;


import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.comac.ins.common.core.exception.base.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.QueryGroup;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdCalculationExampleBo;
import com.comac.ins.isfd.domain.vo.CalculationExampleCountObjectVo;
import com.comac.ins.isfd.domain.vo.CalculationExampleCountResultObjectVo;
import com.comac.ins.isfd.domain.vo.IsfdCalculationExampleVo;
import com.comac.ins.isfd.service.IIsfdCalculationExampleService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;

/**
 * 算例库controller
 *
 * @author zhouyaoxin
 * @date 2024-7-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/calculationExample")
public class IsfdCalculationExampleController extends BaseController {

    @Autowired
    private IIsfdCalculationExampleService IIsfdCalculationExampleService;

    /**
     * 按算例数据文件类型,统计算例数据数量
     */
    @SaCheckPermission("isfd:calculationExample:count")
    @GetMapping("/list/count")
    public R<List<CalculationExampleCountObjectVo>> listCount() {
        return R.ok(IIsfdCalculationExampleService.listCount());
    }

    /**
     * 按算例数据文件类型,统计算例数据数量
     */
    @SaCheckPermission("isfd:calculationExample:count")
    @GetMapping("/list/count/{field}")
    public R<List<CalculationExampleCountResultObjectVo>> listCountByListByField(@PathVariable String field) {
        return R.ok(IIsfdCalculationExampleService.listCountByListByField(field));
    }

    /**
     * 按算例数据文件时间,统计算例数据数量
     */
    @SaCheckPermission("isfd:calculationExample:count")
    @GetMapping("/list/time/count")
    public R<List<CalculationExampleCountResultObjectVo>> listTimeCount(
        @RequestParam(value = "field", required = false) String field,
        @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
        @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        return R.ok(IIsfdCalculationExampleService.listTimeCount(field, startTime, endTime));
    }


    /**
     * 算例库分页列表查询
     */
    @SaCheckPermission("isfd:calculationExample:list")
    @GetMapping("/list")
    public TableDataInfo<IsfdCalculationExampleVo> list(@Validated(QueryGroup.class) IsfdCalculationExampleBo bo, PageQuery pageQuery) {
        return IIsfdCalculationExampleService.queryPageList(bo, pageQuery);
    }

    /**
     * 算例库新增保存
     *
     * @param file 文件
     */
    @SaCheckPermission("isfd:calculationExample:save")
    @Log(title = "上传calculationExample + 详细信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<IsfdCalculationExampleVo> save(@RequestParam("file") MultipartFile file,
                                            @RequestParam("displayName") String displayName,
                                            @RequestParam("analysisType") String analysisType,
                                            @RequestParam("calculationExampleType") String calculationExampleType) {
        if(file == null || file.isEmpty()){
            throw new BaseException("上传的算例文件不能为空，请确认后重试！");
        }
        IsfdCalculationExampleBo isfdCalculationExampleBo = new IsfdCalculationExampleBo();
        isfdCalculationExampleBo.setDisplayName(displayName);
        isfdCalculationExampleBo.setCalculationExampleType(calculationExampleType);
        isfdCalculationExampleBo.setAnalysisType(analysisType);
        IsfdCalculationExampleVo isfdCalculationExampleVo = IIsfdCalculationExampleService.save(file, isfdCalculationExampleBo);
        return R.ok(isfdCalculationExampleVo);
    }

    /**
     * 算例库下载文件
     *
     * @param calculationExampleId 算例库id
     */
    @SaCheckPermission("isfd:calculationExample:download")
    @GetMapping("/download/{calculationExampleId}")
    public void download(@PathVariable Long calculationExampleId, HttpServletResponse response) throws IOException {
        IIsfdCalculationExampleService.download(calculationExampleId, response);
    }

    /**
     * 删除算例库对象
     *
     * @param calculationExampleIds 算例库id数组
     */
    @SaCheckPermission("isfd:calculationExample:remove")
    @Log(title = "算例库删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{calculationExampleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] calculationExampleIds) {
        return toAjax(IIsfdCalculationExampleService.deleteWithValidByIds(List.of(calculationExampleIds), true));
    }

    /**
     * 更新算例库对象
     *
     * @param isfdCalculationExampleBo 算例库对象
     */
    @SaCheckPermission("isfd:calculationExample:remove")
    @Log(title = "算例库更新")
    @PostMapping("/update")
    public R<Void> remove(@RequestBody IsfdCalculationExampleBo isfdCalculationExampleBo) {
        if (isfdCalculationExampleBo.getCalculationExampleId() == null) {
            return R.fail("id不能为空");
        }
        IIsfdCalculationExampleService.update(isfdCalculationExampleBo);
        return R.ok();
    }
}
